//
//  DDVisitorView.swift
//  swift微博
//
//  Created by whitcast on 16/7/9.
//  Copyright © 2016年 whitcast. All rights reserved.
//

import UIKit
import SnapKit

class DDVisitorView: UIView {
    //block回调
    var loginCallback :(()->())?
    
    var registerCallback :(()->())?
    
    //懒加载  旋转的圆圈视图
    lazy var circleImg :UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    //懒加载  中间视图
    lazy var centerImg :UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    //遮挡视图
    lazy var coveryView :UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    //文本框
    lazy var textLab :UILabel = {
        
        let lab = UILabel()
        
        lab.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        //字体大小
        lab.font = UIFont.systemFontOfSize(14)
        //自动换行
        lab.numberOfLines = 0
        
        return lab
    }()
    
    //注册按钮
    lazy var loginBtn :UIButton = {
        
        let btn = UIButton()
        
        btn.setTitle("注册", forState: UIControlState.Normal)
        
        btn.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        //点击事件
        btn.addTarget(self, action: "loginClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return btn
        
    }()
    //登录按钮
    lazy var registerLogin :UIButton = {
        
        let btn = UIButton()
        
        btn.setTitle("登录", forState: UIControlState.Normal)
        
        btn.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        //点击事件
        btn.addTarget(self, action: "registerBtnClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return btn
        
    }()
    
    //登录
    func loginClick() {
        
        print("登录")
        
        loginCallback?()
        
    }
    
    //注册
    func registerBtnClick() {
        
        print("注册")
        
        registerCallback?()
        
    }
    
    override init(frame: CGRect){
        super.init(frame: frame)
        
        addSubview(circleImg)
        
        addSubview(centerImg)
        
        addSubview(coveryView)
        
        addSubview(textLab)
        
        addSubview(loginBtn)
        
        addSubview(registerLogin)
        //开启动画
        startAnimationForCircle()
        //背景颜色
        backgroundColor = UIColor(white: 237.0/255.0, alpha: 1)
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    override func layoutSubviews() {
        super.layoutSubviews()
        //旋转视图的大小设置
        circleImg.sizeToFit()
        
        circleImg.center = center
        
        circleImg.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        
        }
        //中间图片的大小
//        centerImg.center = center
//        
//        centerImg.sizeToFit()
    
        centerImg.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(circleImg)
        }
        //遮挡视图的大小
        coveryView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(centerImg)
            make.width.equalTo(235)
        }
        //文本框大小
        textLab.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(coveryView)
            make.trailing.equalTo(coveryView)
            make.top.equalTo(coveryView.snp_bottom).offset(10)
            
        }
        //注册按钮
        loginBtn.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(coveryView)
            make.top.equalTo(textLab.snp_bottom).offset(10)
            make.width.equalTo(100)
        }
        //登录按钮
        registerLogin.snp_makeConstraints { (make) -> Void in
            make.trailing.equalTo(coveryView)
            make.top.equalTo(textLab.snp_bottom).offset(10)
            make.width.equalTo(100)
        }
    }
    
    //开启动画
    func startAnimationForCircle() {
        
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        //设置动画
        animation.toValue = 2 * M_PI
        
        animation.duration = 20
        
        animation.repeatCount = MAXFLOAT
        //设置如下,以免回到后台后动画停止
        animation.removedOnCompletion = false
        
        circleImg.layer.addAnimation(animation, forKey: nil)
        
    }
    
    //初始化控制器
    func setUPViewVC(title: String?,centerImgName: String?) {
        if centerImgName != nil  && title != nil{
            
            centerImg.image = UIImage(named: centerImgName!)
            
            textLab.text = title!
            //隐藏旋转视图
            circleImg.hidden = true
            
        }else {
            //取消隐藏视图
            circleImg.hidden = false
        }
    }
    
}
